<template>
  <div class="register-container">
    <div class="register-box">
      <h2>注册小米有品</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <input 
            type="text" 
            v-model="username" 
            placeholder="用户名"
            required
          >
        </div>
        <div class="form-group">
          <input 
            type="email" 
            v-model="email" 
            placeholder="邮箱"
            required
          >
        </div>
        <div class="form-group">
          <input 
            type="password" 
            v-model="password" 
            placeholder="密码"
            required
          >
        </div>
        <div class="form-group">
          <input 
            type="password" 
            v-model="confirmPassword" 
            placeholder="确认密码"
            required
          >
        </div>
        <div class="error-message" v-if="error">{{ error }}</div>
        <button type="submit" class="register-btn">注册</button>
      </form>
      <div class="links">
        <router-link to="/login">已有账号？立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import userData from '@/assets/users.json'

export default {
  data() {
    return {
      username: '',
      email: '',
      password: '',
      confirmPassword: '',
      error: ''
    }
  },
  methods: {
    handleRegister() {
      if (this.password !== this.confirmPassword) {
        this.error = '两次输入的密码不一致'
        return
      }

      const userExists = userData.users.some(
        u => u.username === this.username || u.email === this.email
      )

      if (userExists) {
        this.error = '用户名或邮箱已存在'
        return
      }

      // 添加新用户
      const newUser = {
        username: this.username,
        email: this.email,
        password: this.password
      }

      userData.users.push(newUser)
      
      // 注册成功后自动登录
      localStorage.setItem('currentUser', JSON.stringify(newUser))
      this.$router.push('/')
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #f5f5f5;
}

.register-box {
  background: white;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  width: 100%;
  max-width: 400px;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
}

input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  outline: none;
}

input:focus {
  border-color: #ff6700;
}

.register-btn {
  width: 100%;
  padding: 12px;
  background: #ff6700;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s;
}

.register-btn:hover {
  background: #f25600;
}

.error-message {
  color: #ff4444;
  margin-bottom: 15px;
  font-size: 14px;
}

.links {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
}

.links a {
  color: #666;
  text-decoration: none;
}

.links a:hover {
  color: #ff6700;
}
</style> 